package com.xinchao.service.impl;

import com.xinchao.core.constant.BusinessConstant;
import com.xinchao.core.constant.RequestConstant;
import com.xinchao.core.exception.ServiceException;
import com.xinchao.core.util.HttpUtils;
import com.xinchao.dao.AreaMapper;
import com.xinchao.model.Area;
import com.xinchao.model.OperationLog;
import com.xinchao.model.User;
import com.xinchao.model.dto.AreaCityDTO;
import com.xinchao.model.dto.AreaDistrictDTO;
import com.xinchao.model.dto.AreaProvinceDTO;
import com.xinchao.model.dto.AreaTreeDTO;
import com.xinchao.model.vo.AreaVO;
import com.xinchao.service.AreaService;
import com.xinchao.service.OperationLogService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dxy
 * @date 2018/8/9 10:27
 */
@Service(value = "areaService")
public class AreaServiceImpl implements AreaService {
	@Autowired
	private AreaMapper areaMapper;
	@Autowired
	private OperationLogService operationLogService;

	/**
	 * 保存省市区
	 *
	 * @param area    Area
	 * @param request HttpServletRequest
	 * @throws ServiceException
	 */
	@Override
	public void saveArea(Area area, HttpServletRequest request) throws ServiceException {
		if (area == null || request == null) {
			throw new ServiceException("输入参数不能为空");
		}
		//保存省市区
		areaMapper.insertArea(area);
		//记录操作日志
		User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
		OperationLog operationLog = new OperationLog();
		operationLog.setOperationContent("保存省市区,省市区ID为:" + area.getAreaId());
		operationLog.setOperationStatus(BusinessConstant.OPERATION_LONG_OPERATION_STATUS_SUCCESS);
		operationLog.setOperationTime(System.currentTimeMillis());
		operationLog.setOperationModule("");
		operationLog.setRealname(loginUser.getRealname());
		operationLog.setUserName(loginUser.getUserName());
		operationLog.setOperationIp(HttpUtils.getRemortIP(request));
		operationLogService.saveOperationLog(operationLog);
	}

	/**
	 * 保存省市区
	 *
	 * @param area    Area
	 * @param request HttpServletRequest
	 * @throws ServiceException
	 */
	@Override
	public void updateArea(Area area, HttpServletRequest request) throws ServiceException {
		if (area == null || request == null) {
			throw new ServiceException("输入参数不能为空");
		}
		areaMapper.updateArea(area);
		//记录操作日志
		User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
		OperationLog operationLog = new OperationLog();
		operationLog.setOperationContent("修改省市区,省市区ID为:" + area.getAreaId());
		operationLog.setOperationStatus(BusinessConstant.OPERATION_LONG_OPERATION_STATUS_SUCCESS);
		operationLog.setOperationTime(System.currentTimeMillis());
		operationLog.setOperationModule("");
		operationLog.setRealname(loginUser.getRealname());
		operationLog.setUserName(loginUser.getUserName());
		operationLog.setOperationIp(HttpUtils.getRemortIP(request));
		operationLogService.saveOperationLog(operationLog);
	}

	/**
	 * 获取所有的省
	 *
	 * @return List<AreaProvinceDTO>
	 */
	@Override
	public List<AreaProvinceDTO> listAllProvince() {
		return areaMapper.listAllProvince();
	}

	/**
	 * 通过省编码获取省下面的所有的市
	 *
	 * @param provinceCode 省编码
	 * @return List<AreaCityDTO>
	 */
	@Override
	public List<AreaCityDTO> listCityByProvince(String provinceCode) {
		if (StringUtils.isNotBlank(provinceCode)) {
			return areaMapper.listCityByProvince(provinceCode);
		}
		return new ArrayList<>();
	}

	/**
	 * 获取部门的城市列表
	 *
	 * @param departmentId 部门ID
	 * @return List<AreaCityDTO>
	 */
	@Override
	public List<AreaCityDTO> listCityByDeparmentId(Long departmentId) {
		if (departmentId == null) {
			return new ArrayList<>();
		} else {
			return areaMapper.listCityByDeparmentId(departmentId);
		}
	}

	/**
	 * 获取所有被选择的城市
	 *
	 * @return List<AreaCityDTO>
	 */
	@Override
	public List<AreaCityDTO> listAllSelectedCity() {
		return areaMapper.listAllSelectedCity();
	}

	@Override
	public Long countSelectedCity(String cityCodes) {
		return null;
	}

	/**
	 * 获取城市的所有行政区
	 *
	 * @param cityCode 城市编码
	 * @return List<AreaDistrictDTO>
	 */
	@Override
	public List<AreaDistrictDTO> listDistrictByCityCode(String cityCode) {
		return areaMapper.listDistrictByCityCode(cityCode);
	}

    /**
     * 省市区下拉联动查询
     * 参数都为空时，查询所有省份，通过省份查市，通过市查县/区
     *
     * @param province 省
     * @param city     市
     * @return List<AreaVO>
     */
	@Override
	public List<AreaVO> list(String province, String city) {
		if(StringUtils.isNotBlank(province)){
			//通过省查询市
			return areaMapper.listCityByProvinceName(province);
		}
		if(StringUtils.isNotBlank(city)){
			//通过市查询区/县
			return areaMapper.listDistrictByCityName(city);
		}
		//查询省
		return areaMapper.listProvince();
	}

	/**
	 * 获取全部城市树状信息
	 * @return
	 */
	@Override
	public  List<AreaTreeDTO> listAllTreeData(){
		return areaMapper.listDataTree();
	}

	@Override
	public AreaTreeDTO getParentByCityCode(String cityCode) {
		return areaMapper.getParentByCityCode(cityCode);
	}

	@Override
	public List<AreaTreeDTO> listAllTreeDataByDepartmentId(Long departmentId) {
		List<AreaTreeDTO> data = areaMapper.listAllTreeDataByDepartmentId(departmentId);
		if(CollectionUtils.isNotEmpty(data)){
			for (int i = 0; i < data.size(); i++) {
				AreaTreeDTO areaTreeDTO = data.get(i);
				if(StringUtils.isNotBlank(areaTreeDTO.getParentId())){
					AreaTreeDTO parent = areaMapper.getParentByCityCode(areaTreeDTO.getId());
					if(null != parent){
						data.add(parent);
					}
				}
			}
		}
		return data;
	}
}
